package com.achilles.learncontentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

public class BookProvider extends ContentProvider {

    public static final String AUTHORITY = "com.achillesl.bookprovider";
    public static final Uri BOOK_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/book");
    public static final Uri USER_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/user");

    public static final int BOOK_URI_CODE = 0;
    public static final int USER_URI_CODE = 1;
    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    private SQLiteDatabase mDb;

    static {
        sUriMatcher.addURI(AUTHORITY,"book",BOOK_URI_CODE);
        sUriMatcher.addURI(AUTHORITY,"user",USER_URI_CODE);
    }

    public BookProvider() {
    }

    private void initProviderData() {
        mDb = new DbOpenHelper(getContext(), DbOpenHelper.DB_NAME, null, DbOpenHelper.DB_VERSION)
                .getWritableDatabase();
        mDb.execSQL("delete from "+ DbOpenHelper.BOOK_TABLE_NAME);
        mDb.execSQL("delete from "+ DbOpenHelper.USER_TABLE_NAME);
        mDb.execSQL("insert into book values(1,'Android');");
        mDb.execSQL("insert into book values(2,'IOS');");
        mDb.execSQL("insert into book values(3,'Html5');");
        mDb.execSQL("insert into book values(4,'jake');");
        mDb.execSQL("insert into book values(5,'jasmine');");
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        Log.d("BookProvider","delete");
        String table = getTableName(uri);
        if (table == null) {
            throw new IllegalArgumentException("Unsupported Uri:" + uri);
        }
        int count = mDb.delete(table,selection,selectionArgs);
        if (count > 0) {
            getContext().getContentResolver().notifyChange(uri,null);
        }
        return count;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        Log.d("BookProvider","getType");
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
        Log.d("BookProvider","insert");
        String table = getTableName(uri);
        if (table == null) {
            throw new IllegalArgumentException("Unsupported Uri:" + uri);
        }
        mDb.insert(table,null,values);
        getContext().getContentResolver().notifyChange(uri,null);
        return uri;
    }

    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        Log.d("BookProvider","onCreate"+Thread.currentThread().getName());
        initProviderData();
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        // TODO: Implement this to handle query requests from clients.
        Log.d("BookProvider","query"+Thread.currentThread().getName());
        String table = getTableName(uri);
        if (table == null) {
            throw new IllegalArgumentException("Unsupported Uri:" + uri);
        }
        return mDb.query(table,projection,selection,selectionArgs,null,null,sortOrder,null);
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        Log.d("BookProvider","update");
        String table = getTableName(uri);
        if (table == null) {
            throw new IllegalArgumentException("Unsupported Uri:" + uri);
        }
        int count = mDb.update(table,values,selection,selectionArgs);
        if (count > 0) {
            getContext().getContentResolver().notifyChange(uri,null);
        }
        return count;
    }

    private String getTableName(Uri uri) {
        String tableName = null;
        switch (sUriMatcher.match(uri)) {
            case BOOK_URI_CODE:{
                tableName = DbOpenHelper.BOOK_TABLE_NAME;
                break;
            }
            case USER_URI_CODE:{
                tableName = DbOpenHelper.USER_TABLE_NAME;
                break;
            }
            default:
                break;
        }
        return tableName;
    }
}
